/*
 * Copyright (c) 2012 
 */
package startup.view;

import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.*;
import startup.model.user.Administrator;

/**
 * Servlet responsible for administrator's search
 * <p/>
 * @author Startup  
 * @author Startup  
 * @author Startup  
 */
public class AdministratorSearchStudent extends HttpServlet
{
	private Administrator administrator;
	private ResultSet result;
	private String searchParameter;
	private Integer studentId;
	private Integer studentFund;
	private String first_name;
	private String last_name;

	/**
	 * Processes requests for both HTTP
	 * <code>GET</code> and
	 * <code>POST</code> methods.
	 * <p/>
	 * @param request servlet request
	 * @param response servlet response
	 * <p/>
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	protected void processRequest( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException, SQLException
	{
		try
		{
			//prendo dalla pagina administratorHome il valore della textarea
			searchParameter = request.getParameter( "studentId" );
			// prendo la handle della sessione
			HttpSession session = request.getSession();

			// prendo la handle del dao (in questo caso un'istanza di Administrator)
			administrator = ( Administrator )session.getAttribute( "currentSessionUser" );

			//chiamo la funzione per la ricerca
			administrator.searchStudent( searchParameter );

			//importo la ricerca dalla pagina administrator.java
			result = administrator.getResultToServlet();

			studentId = result.getInt( "user_ID" );
			studentFund = result.getInt( "personal_funds_available" );
			first_name = result.getString( "first_name" );
			last_name = result.getString( "last_name" );

			// creo per scrivere la pagina JSP
			Cookie userIdCookie = new Cookie( "userId", studentId.toString() );
			Cookie studentFundCookie = new Cookie( "studentFund", studentFund.toString() );
			Cookie studentFNameCookie = new Cookie( "first_name", first_name.toString() );
			Cookie studentLNameCookie = new Cookie( "last_name", last_name.toString() );

			// -1 = durata della sessione
			userIdCookie.setMaxAge( -1 );
			studentFundCookie.setMaxAge( -1 );
			studentFNameCookie.setMaxAge( -1 );
			studentLNameCookie.setMaxAge( -1 );

			// aggioungo i cookie alla sessione
			response.addCookie( userIdCookie );
			response.addCookie( studentFundCookie );
			response.addCookie( studentFNameCookie );
			response.addCookie( studentLNameCookie );

			// redirect alla pagina desiderata
			response.sendRedirect( "administratorModify.jsp" );
		}
		catch( RuntimeException exception )
		{
			request.setAttribute( "exception", exception );
			request.getRequestDispatcher( "errorPage.jsp" ).forward( request, response );
		}
	}

	/**
	 * Handles the HTTP
	 * <code>GET</code> method.
	 * <p/>
	 * @param request servlet request
	 * @param response servlet response
	 * <p/>
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doGet( HttpServletRequest request, HttpServletResponse response )
		throws ServletException, IOException
	{
		try
		{
			processRequest( request, response );
		}
		catch( SQLException ex )
		{
			Logger.getLogger( AdministratorSearchStudent.class.getName() ).log( Level.SEVERE, null, ex );
		}
	}

	/**
	 * Handles the HTTP
	 * <code>POST</code> method.
	 * <p/>
	 * @param request servlet request
	 * @param response servlet response
	 * <p/>
	 * @throws ServletException if a servlet-specific error occurs
	 * @throws IOException if an I/O error occurs
	 */
	@Override
	protected void doPost( HttpServletRequest request, HttpServletResponse response ) throws ServletException, IOException
	{
		try
		{
			processRequest( request, response );
		}
		catch( SQLException ex )
		{
			Logger.getLogger( AdministratorSearchStudent.class.getName() ).log( Level.SEVERE, null, ex );
		}
	}

}